<?php

namespace app\modules\log\models;

use app\common\base\CommonModel;
use yii\helpers\ArrayHelper;

class DeviceLogs extends CommonModel
{
    // 数据库表名设置
    public static function tableName()
    {
        return '{{%device_logs}}';
    }

    /**
     * rules
     *
     * @return array
     */
    public function rules()
    {
        return [
            [['userId', 'deviceId', 'operationId', 'timestamp'], 'required'],
            [['userId', 'deviceId', 'operationId', 'timestamp'], 'integer'],
            ['message', 'string'],
        ];
    }


    public function behaviors()
    {
        return parent::behaviors();
    }

    /**
     * beforeSave
     *
     * @param bool $insert
     * @return void
     */
    public function beforeSave($insert)
    {
        if ($insert) {
            // $this->status = 1;
        }

        return parent::beforeSave($insert);
    }


    public function fields()
    {
        $fields = parent::fields();

        //创建时间戳
        if (isset($fields['timestamp'])) {
            $fields['time'] = function ($model) {
                return date("Y-m-d H:i", $model->timestamp / 1000);
            };
        }
        return $fields;
    }
}
